package com.chancingpack.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;

public class ContextUtil implements ApplicationContextAware{

    private static ApplicationContext context;
    private static   ThreadLocal<String> sessionId=new  ThreadLocal<String>();

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext(){
        return context;
    }

    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }

    public static Object getBean(Class clss) {
        return context.getBean(clss);
    }

    public static void setSessionId(String id) {
        sessionId.set(id);
    }

    public static String getSessionId( ) {
        String id=sessionId.get();
        if(StringUtils.isEmpty(id)){
            throw new  RuntimeException("获取浏览器sessionId异常");
        }
        return id;
    }
}